package com.p2p.auth.service;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.util.List;
import java.util.Map;

/**
 * Excel 工具类
 *
 * @author Evan
 */
public class ExcelService {

    private Logger logger = LoggerFactory.getLogger(ExcelService.class);

    private SXSSFWorkbook wb = null;
    private SXSSFSheet sheet = null;
    private CellStyle cellstyle = null;

    public ExcelService(List<?> body, Map<String, String> struct) {
        this.wb = new SXSSFWorkbook(1000);
        this.sheet = wb.createSheet("Sheet1");
        this.cellstyle = wb.createCellStyle();

        createHead(struct);
        createBody(body, struct);
    }

    private void createHead(Map<String, String> struct) {
        int col = 0;

        for (String field : struct.keySet()) {
            createCell(0, col, struct.get(field));
            col++;
        }

    }

    private void createBody(List<?> body, Map<String, String> struct) {
        for (int row = 0; row < body.size(); row++) {

            try {
                int col = 0;
                for (String field : struct.keySet()) {
                    Object bean = body.get(row);
                    String cellValue = BeanUtils.getProperty(bean, field);
                    createCell(row + 1, col, cellValue);
                    col++;
                }

            } catch (Exception e) {
                e.printStackTrace();
            }

        }

    }

    private void createCell(int rowNum, int colNum, String val) {
        SXSSFRow row = sheet.getRow(rowNum);
        if (row == null) {
            row = sheet.createRow(rowNum);
        }

        SXSSFCell cell = row.createCell(colNum);
        cell.setCellType(HSSFCell.ENCODING_UTF_16);
        cell.setCellValue(val);

        // cellstyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);
        cell.setCellStyle(cellstyle);
    }

    public Workbook getWorkBook() {
        return wb;
    }

    public void exportToStream(HttpServletResponse response, String title) {
        response.setContentType("application/vnd.ms-excel");

        try {
            response.setHeader("Content-disposition", "attachment; filename=" + new String((title).getBytes("gbk"), "iso8859-1") + ".xlsx");
            BufferedOutputStream outputStream = new BufferedOutputStream(response.getOutputStream());

            this.wb.write(outputStream);

            outputStream.flush();
            outputStream.close();
            wb.close();
        } catch (Exception ex) {
            logger.error("导出异常!",ex);
        }
    }
}
